package org.groupg.project.config;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@JsonPropertyOrder({"ports"})
public class JsonProxyConfig {
    @JsonProperty("ports")
    private List<JsonPortConfig> ports;

    // 从ProxyConfig转换
    public static JsonProxyConfig fromProxyConfig(ProxyConfig config) {
        JsonProxyConfig jsonConfig = new JsonProxyConfig();
        jsonConfig.ports = config.getPortRules().entrySet().stream()
            .map(entry -> new JsonPortConfig(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());
        return jsonConfig;
    }

    // 转换为ProxyConfig
    public ProxyConfig toProxyConfig() {
        ProxyConfig config = new ProxyConfig();
        Map<Integer, List<RuleConfig>> portRules = ports.stream()
            .collect(Collectors.toMap(
                JsonPortConfig::getPort,
                JsonPortConfig::getRules
            ));
        config.setPortRules(portRules);
        return config;
    }

    // Getters and Setters
    public List<JsonPortConfig> getPorts() {
        return ports;
    }

    public void setPorts(List<JsonPortConfig> ports) {
        this.ports = ports;
    }
}
